<?php
class ModelFidoAdvertisement extends Model {
	public function addAdvertisement($data) {
		$this->db->query("INSERT INTO ". DB_PREFIX ."logo SET logo_file='" . $this->db->escape($data['image']) .  "',logo_width= '". (int)$data['logo_width'] .  "',logo_height= '". (int)$data['logo_height'] .  "',site_url= '". $this->db->escape($data['site_url']) ."',start_date='". $this->db->escape($data['start_date']) ."',end_date='". $this->db->escape($data['end_date']) .  "',enabled='".(int)$data['status']."'");
		$logo_id = $this->db->getLastId();
		foreach ($data['site_name'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "logo_description SET logo_id = '" . (int)$logo_id . "', language_id = '" . (int)$language_id . "', site_name = '" . $this->db->escape($value['name']) . "'");
		}
		if (isset($data['logo_store'])) {
			foreach ($data['logo_store'] as $store_id) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "logo_to_store SET logo_id = '" . (int)$logo_id . "', store_id = '" . (int)$store_id . "'");
			}
		}
		$this->cache->delete('logo');
	}

	public function editAdvertisement($logo_id, $data) {
		$this->db->query("UPDATE " . DB_PREFIX . "logo SET logo_file='" . $this->db->escape($data['image']) .  "',logo_width= '". (int)$data['logo_width'] .  "',logo_height= '". (int)$data['logo_height'] .  "',site_url= '". $this->db->escape($data['site_url']) ."',start_date='". $this->db->escape($data['start_date']) ."',end_date='". $this->db->escape($data['end_date']) .  "',enabled = '" . (int)$data['status'] . "', logo_active = '" . (int)$this->request->post['active'] . "' WHERE logo_id = '" . (int)$logo_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "logo_description WHERE logo_id = '" . (int)$logo_id . "'");
		foreach ($data['site_name'] as $language_id => $value) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "logo_description SET logo_id = '" . (int)$logo_id . "',site_name='". $this->db->escape($value['name'])."', language_id = '" . (int)$language_id . "'");
		}
		$this->db->query("DELETE FROM " . DB_PREFIX . "logo_to_store WHERE logo_id = '" . (int)$logo_id . "'");
		if (isset($data['logo_store'])) {
			foreach ($data['logo_store'] as $store_id) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "logo_to_store SET logo_id = '" . (int)$logo_id . "', store_id = '" . (int)$store_id . "'");
			}
		}
		$this->cache->delete('logo');
	}

	public function deleteAdvertisement($logo_id) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "logo WHERE logo_id = '" . (int)$logo_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "logo_description WHERE logo_id = '" . (int)$logo_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "logo_to_store WHERE logo_id = '" . (int)$logo_id . "'");
		$this->cache->delete('logo');
	}	

	public function getAdvertisement($logo_id) {
		$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "logo WHERE logo_id = '" . (int)$logo_id . "'");
		return $query->row;
	}

	public function getAdvertisementDescriptions($logo_id) {
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "logo_description WHERE logo_id = '" . (int)$logo_id . "'");
		foreach ($query->rows as $result) {
			$logo_description_data[$result['language_id']] = array(
				 'name'=> $result['site_name']
			);
		}
		return $logo_description_data;
	}

	public function getAdvertisements() {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "logo p LEFT JOIN " . DB_PREFIX . "logo_description pd ON (p.logo_id = pd.logo_id) WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY p.start_date");
		return $query->rows;
	}

	public function getTotalAdvertisements() {
     	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "logo");
		return $query->row['total'];
	}

	public function checkActiveAdvertisement() {
     	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "logo WHERE logo_active = '1'");
		return $query->row['total'];
	}

	public function getActiveAdvertisement() {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "logo WHERE logo_active = '1'");
		return $query->row;
	}

	public function getAdvertisementData($logo_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "logo p LEFT JOIN " . DB_PREFIX . "logo_description pd ON (p.logo_id = pd.logo_id) WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p.logo_id = '" . (int)$logo_id . "'");
		return $query->row;
	}

	public function getAdvertisementResults($logo_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "logo_reactions WHERE logo_id = '" . (int)$logo_id . "'");
		return $query->rows;
	}

	public function getTotalResults($logo_id) {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "logo_reactions WHERE logo_id = '" . (int)$logo_id . "'");
		return $query->row['total'];
	}

	public function getAdvertisementStores($logo_id) {
		$logo_store_data = array();
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "logo_to_store WHERE logo_id = '" . (int)$logo_id . "'");
		foreach ($query->rows as $result) {
			$logo_store_data[] = $result['store_id'];
		}
		return $logo_store_data;
	}

	public function checkAdvertisements() {
		$create_logo = "CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "logo` ( `logo_id` int(11) unsigned NOT NULL AUTO_INCREMENT, `logo_file` varchar(128) NOT NULL,`logo_width` smallint(3) DEFAULT NULL,`logo_height` smallint(3) DEFAULT NULL,`site_url` varchar(128) NOT NULL,`hits` int(11) unsigned NOT NULL DEFAULT '0',`start_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',`end_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `logo_order` int(11) unsigned NOT NULL DEFAULT '1',`logo_pos` int(11) NOT NULL,`logo_pages` text,`logo_active` int(1) unsigned NOT NULL DEFAULT '1',`enabled` tinyint(1) unsigned NOT NULL DEFAULT '0',PRIMARY KEY (`logo_id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;";
		$this->db->query($create_logo);
		$create_logo_descriptions = "CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "logo_description` ( `logo_id` int(11) NOT NULL, `site_name` varchar(128) NOT NULL, `language_id` int(11) NOT NULL,PRIMARY KEY (`logo_id`,`language_id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;";
		$this->db->query($create_logo_descriptions);		
		$create_logo_to_store = "CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "logo_to_store` (`logo_id` int(11) NOT NULL, `store_id` int(11) NOT NULL, PRIMARY KEY  (`logo_id`, `store_id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;";
		$this->db->query($create_logo_to_store);
	}
}
?>
